<?php


namespace App\Services;



use App\Tools\CurlTools;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Factory;
class WechatService
{

        protected $app;
        /**
         * WechatService constructor.
         * 实例化Factory
         */
        public function __construct()
        {
            $this->app=Factory::officialAccount(config("wechats"));
        }

        /**
         * @Notes:
         * @Author: 遇见
         * @Date: 2022/11/23
         * @Time: 21:31
         * @param $request
         * @param $params
         * @return \Symfony\Component\HttpFoundation\Response
         * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
         * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
         * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
         * 验签 发消息 二级菜单
         */
        public function checkToken(){
            $this->secondaryMenu();//创建二级菜单
            $this->TypeMessage();//返回信息
            return $this->app->server->serve();
        }

        /**
         * @Notes:
         * @Author: 遇见
         * @Date: 2022/11/23
         * @Time: 21:32
         * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
         * @throws \ReflectionException
         * 返回的消息
         */
        public  function TypeMessage(){
            $this->app->server->push(function ($message) {
               return MessageService::typeMessage($message);
            });
        }


        /**
         * @Notes:
         * @Author: 遇见
         * @Date: 2022/11/23
         * @Time: 13:55
         * @return mixed
         * 二级菜单
         */
        public function secondaryMenu(){
            $buttons = [
                [
                    "type" => "click",
                    "name" => "客户端",
                    "key"  => "V1001_TODAY_MUSIC",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "后台",
                            "url"  => "http://www.cyjyyds.cn:9090/"
                        ],
                    ]
                ],
                [
                    "name"       => "菜单",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "搜索",
                            "url"  => "http://www.soso.com/"
                        ],
                        [
                            "type" => "view",
                            "name" => "视频",
                            "url"  => "http://v.qq.com/"
                        ],
                        [
                            "type" => "view",
                            "name" => "二维码",
                            "url" => "http://www.xumengdi.com/wechat/qr"
                        ],
                    ],
                ],
            ];

            $aa=$this->app->menu->create($buttons); Log::info($aa);
        }
        public function qRCode(){
            $result = $this->app->qrcode->forever(56);
            $url = $this->app->qrcode->url($result['ticket']);
            return view('wechat',['data'=>$url]);
        }
//        public function updateFiles($file){
//            $fileUrl = public_path().'/zip'.'/'.$file;
//            $path = new \CURLFile($fileUrl);
//            $data =[
//                'media'=>$path
//            ];
//            $accessToken = $this->getAccessToken();
//            $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken}&type=image";
//            $postUrl =CurlTools::postUrl($url,$data); Log::info($postUrl);
//            return $postUrl['media_id'];
//        }
}
